package GUI;

import java.awt.Color;
import java.awt.HeadlessException;

import javax.swing.JOptionPane;

import Client_logic.Type;
import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;

/**
 * This class use for update and insert type to data base.
 * @author michael
 *
 */
public class TypeUpdateForm extends TypeManagementForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean addflag;
	private boolean updateflag;
	private boolean removeflag;
	private Type type = null; 
	
	public TypeUpdateForm(){
		super();
		getSectionsIDLabel().setText("Choose Category");
		initialize(); 
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() {
		type = new Type();
		addflag = false;
		updateflag = false;
		removeflag = false;
		getIdLabel().setText("Type ID");
		getNameLabel().setText("Type name");
		getTypesIDLabel().setText("Choose Tybe");
		getProductsIDLabel().setText("All product in type");
		getAddbuttonLabel().setText("Selected type info:");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		addSaveButton();
		addUpdateButton();
		addremoveButton();
		updateForm();				
		getTypesIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				String str = (String)getTypesIDComboBox().getSelectedItem();
				if (str.equals(":"))
					return;
				String typeID = str.substring(0, str.indexOf(":"));
				getIdTextField().setText(typeID);
				getNameTextField().setText(str.substring(str.indexOf(":")+1, str.length()));
				String[] productslist = null;
				try{
					ConnectionToServer connectiontoserver = new ConnectionToServer();		
					productslist = (String[]) connectiontoserver.loadTypeProduct
					(Integer.parseInt(typeID));
					if (productslist == null){
						getProductsIDTextArea().setText("");
					}							
					else{
						String tmpstr = "";
						int len = productslist.length;
						for(int i=0; i < len; i++){
							tmpstr = tmpstr+(productslist[i]+"\n");
						}
						getProductsIDTextArea().setText(tmpstr);
					}
				}
				catch(ClientNotInitializesException exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception except){
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
			}
		});		
	}
	
	/**
	 * This method update form comboBoxes to data from data base
	 */
	public void updateForm(){
		updateTypesIDComboBox();
		updateSectionsIDComboBox();
	}
		
	/**
	 * This method initializes SaveButton ActionListener
	 */
	private void addSaveButton(){
		getSaveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (addflag){
					saveType();										
				}
				else{
					getSaveButton().setText("OK");
					getAddbuttonLabel().setText("PLZ enter type ID and Name:");
					getNameTextField().setEditable(true);
					getIdTextField().setEditable(true);
					getUpdateButton().setEnabled(false);
					getRemoveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					addflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method initializes updateButton ActionListener
	 */
	private void addUpdateButton(){
		getUpdateButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (updateflag){
					updateType();	
				}
				else{
					getUpdateButton().setText("OK");
					getAddbuttonLabel().setText("PLZ enter new type name:");
					getNameTextField().setEditable(true);
					getRemoveButton().setEnabled(false);
					getSaveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					updateflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method update selected section
	 */
	private void updateType(){
		int typeID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			return;
		}					
		String typeName = getNameTextField().getText();
		if (typeID > 0 && !typeName.equals("")){
			type.setTypeID(typeID);
			type.setTypeName(typeName);
			try {
				if (type.updateType()){
					String oldname = (String)getTypesIDComboBox().getSelectedItem();
					oldname = oldname.substring(oldname.indexOf(":")+1, oldname.length());
					getTypesIDComboBox().removeItem(typeID+":"+oldname);
					getTypesIDComboBox().addItem(typeID+":"+typeName);
					JOptionPane.showMessageDialog(null, "Type successfuly update");
					restore();			
				}
				else
					JOptionPane.showMessageDialog(null, "Type not update!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
			JOptionPane.showMessageDialog(null, "PLZ enter type name!","",JOptionPane.WARNING_MESSAGE);
	}
	
	
	/**
	 * This method add entered type to data base
	 */
	private void saveType(){
		int typeID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			JOptionPane.showMessageDialog(null, "Ilegal ID","",JOptionPane.WARNING_MESSAGE);
			return;
		}					
		String typeName = getNameTextField().getText();
		if (typeID > 0 && !typeName.equals("")){
			type.setTypeID(typeID);
			type.setTypeName(typeName);
			try {
				if (type.addType()){
					JOptionPane.showMessageDialog(null, "Type successfuly saved");
					getTypesIDComboBox().addItem(typeID+":"+typeName);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Type not saved!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
			JOptionPane.showMessageDialog(null, "Ilegal input!","",JOptionPane.WARNING_MESSAGE);		
	}
	
	/**
	 * This method initializes removeButton ActionListener
	 */
	private void addremoveButton(){
		getRemoveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				if (removeflag){
					removeType();	
					restore();
				}
				else{
					getRemoveButton().setText("OK");
					getAddbuttonLabel().setText("Remove this type:");
					getUpdateButton().setEnabled(false);
					getSaveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					removeflag = true;
				}
			}				
		});		
	}
	
	/**
	 * This method remove selected type from data base
	 */
	private void removeType(){
		int typeID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			return;
		}
		
		type.setTypeID(typeID);
		try {
			if (type.removeType()){
				getTypesIDComboBox().removeItem(typeID+":"+getNameTextField().getText());
				JOptionPane.showMessageDialog(null, "Type successfuly remove");					
			}
			else
				JOptionPane.showMessageDialog(null, "Type not remove!!!","",JOptionPane.WARNING_MESSAGE);
		} 
		catch (HeadlessException e1) {
			JOptionPane.showMessageDialog(null, "System ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			
		} 
		catch (ClientNotInitializesException e1) {
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
		};
	}
	
	/**
	 * This method restore the form button  
	 */
	public void restore(){
		getSaveButton().setText("Add new");	
		getUpdateButton().setText("UPDATE");	
		getRemoveButton().setText("Remove");
		getAddbuttonLabel().setText("Selected type info:");	
		addflag = false;
		updateflag = false;
		removeflag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getUpdateButton().setEnabled(true);
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);
	}
}
